package com.hmall.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.user.domain.dto.SetUserRoleDTO;
import com.hmall.user.domain.po.SysUserRoles;
import com.hmall.user.service.SysUserRolesService;
import com.hmall.user.mapper.SysUserRolesMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author yangfeng
* @description 针对表【sys_user_roles(用户和角色关联表)】的数据库操作Service实现
* @createDate 2024-12-17 16:36:29
*/
@Service
public class SysUserRolesServiceImpl extends ServiceImpl<SysUserRolesMapper, SysUserRoles>
    implements SysUserRolesService{

    @Override
    public Boolean setUserRole(SetUserRoleDTO dto) {
        List<Integer> roleIds = dto.getRoleIds();
        LocalDateTime now = LocalDateTime.now();
        List<SysUserRoles> userRoles = roleIds.stream().map(r -> {
            SysUserRoles sysUserRoles = new SysUserRoles();
            sysUserRoles.setRoleId(r);
            sysUserRoles.setUserId(dto.getUserId());
            sysUserRoles.setAssignedAt(now);
            return sysUserRoles;

        }).collect(Collectors.toList());
        return saveBatch(userRoles);
    }
}




